<?php
declare(strict_types=1);

namespace Service\HelperService;

class Str{
    /**
     * 获取指定长度的随机字母数字组合的字符串
     *
     * @param  int $length
     * @return string
     */
    public static function random($length = 16,string $pool = '')
    {
        $pool = !empty($pool) ? $pool : '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        
        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
    }

    /**
     * 将字符串中间部分隐藏为***
     *
     * @param  string $str
     * @return string
     */
    public static function make_star($str = '')
    {
        if($str){
            $strlength=mb_strlen($str,'utf-8');
            $length=$strlength>2?$strlength-2:1;  
            $str=str_replace(mb_substr($str, 1, $length,'utf-8'), str_repeat('*', $length), $str);
        }
        return $str;
    }

    public static function has_special_char($str)
    {
        $len=mb_strlen($str);
        for($i=0;$i<$len;$i++)
        {
            $new_str=mb_substr($str,$i,1,'utf-8');
            $asc=ord($new_str);
            if ($asc < 160) //非中文
            {
                if ($asc >= 48 && $asc <= 57) {
    
                } elseif ($asc >= 65 && $asc <= 90) {
    
                } elseif ($asc >= 97 && $asc <= 122) {
    
                } else {
                    return true; //其他
                }
            } else //中文
            {
                if (!preg_match("/^[\x7f-\xff]/", $new_str))
                {
                    return true;
                }
            }
        }
        return false;
    }
}